import * as coordtransform from 'coordtransform';
import proj from 'ol/proj';
/**
 * 
 */

export type Projection = 'EPSG:4326' | 'EPSG:3857' | 'bd09' | 'gcj02';

export function coordinateTransform(coordinate: number[], source: Projection, destination: Projection): number[] {
    switch(source) {
        case 'EPSG:4326': {
            return transformEPSGbsTo(coordinate, destination);
        }
        case 'EPSG:3857': {
            return transformEPSGwbmktTo(coordinate, destination);
        }
        case 'bd09': {
            return transformBdTo(coordinate, destination);
        }
        case 'gcj02': {
            return transformGcjTo(coordinate, destination);
        }
        default: {
            return coordinate;
        }
    }
}

export function transformEPSGbsTo(coordinate: number[], destination: Projection): number[] {
    switch(destination) {
        case 'EPSG:3857': {
            return proj.transform(coordinate, 'EPSG:4326', 'EPSG:3857');
        }
        case 'bd09': {
            coordinate = transformEPSGbsTo(coordinate, 'gcj02');
            return transformGcjTo(coordinate, destination);
        }
        case 'gcj02': {
            return coordtransform.wgs84togcj02(coordinate[0], coordinate[1]);
        }
        default: {
            return coordinate;
        }
    }
}

export function transformEPSGwbmktTo(coordinate: number[], destination: Projection): number[] {
    switch(destination) {
        case 'EPSG:4326': {
            return proj.transform(coordinate, 'EPSG:3857', 'EPSG:4326');
        }
        case 'bd09': {
            coordinate = transformEPSGwbmktTo(coordinate, 'gcj02');
            return transformGcjTo(coordinate, destination);
        }
        case 'gcj02': {
            coordinate = transformEPSGwbmktTo(coordinate, 'EPSG:4326');
            return transformEPSGbsTo(coordinate, destination);
        }
        default: {
            return coordinate;
        }
    }
}

export function transformGcjTo(coordinate: number[], destination: Projection): number[] {
    switch(destination) {
        case 'EPSG:4326': {
            return coordtransform.gcj02towgs84(coordinate[0], coordinate[1]);
        }
        case 'EPSG:3857': {
            coordinate = transformGcjTo(coordinate, 'EPSG:4326');
            return transformEPSGbsTo(coordinate, destination);
        }
        case 'bd09': {
            return coordtransform.gcj02tobd09(coordinate[0], coordinate[1]);
        }
        default: {
            return coordinate;
        }
    }
}

export function transformBdTo(coordinate: number[], destination: Projection): number[] {
    switch(destination) {
        case 'EPSG:4326': {
            coordinate = transformBdTo(coordinate, 'gcj02');
            return transformGcjTo(coordinate, destination);
        }
        case 'EPSG:3857': {
            coordinate = transformBdTo(coordinate, 'gcj02');
            return transformGcjTo(coordinate, destination);
        }
        case 'gcj02': {
            return coordtransform.bd09togcj02(coordinate[0], coordinate[1]);
        }
        default: {
            return coordinate;
        }
    }
}